package com.shenbianyun.cloudcaring;

import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.utils.DESEncryption;
import com.shenbianyun.bosskgpay.utils.SnowflakeIdGeneratorFactory;
import okhttp3.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author ：张一凡
 * @date ：2025年04月08日 18:44:05
 * @description： http-企业门户-额度发放
 */
public class GrantTest  extends AbstractJavaSamplerClient {

    // 测试
    public static final String getBatchIdUrl = "http://192.168.40.85:9300/cloud-caring/api/employee/quota/save";
    public static final String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDY2MTgxMTksInVzZXJuYW1lIjoiMTcwNDEifQ.S7ULqOAzGCiu_JxRJgt1f5ZN8RDDdWAALrpym8O32zc";
    public static final String grantUrl = "http://192.168.40.85:9300/cloud-caring/api/employee/quota/confirm/grant";
    public static final String passWord = "000048";
    public static final String recycleUrl = "http://192.168.40.85:9300/cloud-caring/api/enterprise/package/recycleEmployeeQuota";
    public static final Long employeeId = 13213L;
    public static final String employeeName = "yvan";
    public static final Long relationId = 27L;
    public static final String transAmt = "0.01";


    // 生产
    //public static final String url = "http://192.168.40.85:9300/cloud-caring/api/employee/quota/save";
    //public static final String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDQwOTkwODksInVzZXJuYW1lIjoiMTcwNDEifQ._q8A9TV5iPbZ_1EGDK2txvh9rOnSyOYNiNkvXHJTKLs";
    //public static final String grantUrl = "http://192.168.40.85:9300/cloud-caring/api/employee/quota/confirm/grant";
    //public static final String passWord = "000048";
    //public static final String recycleUrl = "http://192.168.40.85:9300/cloud-caring/api/enterprise/package/recycleEmployeeQuota";
    //public static final Long employeeId = 13213L;
    //public static final String employeeName = "yvan";
    //public static final Long relationId = 27L;
    //public static final String transAmt = "0.01";

    public static void main(String[] args) {

        try {
            GrantTest a = new GrantTest();
            a.grant();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //设置可用参数及的默认值；
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("access_token", token);//设置参数，员工编号
        return params;
    }

    public String grant() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("batchId", SnowflakeIdGeneratorFactory.getInstance().nextId());
        String payPwd = DESEncryption.encryptPassword(passWord);
        map.put("payPwd", payPwd);
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
                //设置连接超时时间
                .readTimeout(20, TimeUnit.SECONDS)//设置读取超时时间
                .build();
        // 创建请求体
        MediaType mediaType = MediaType.parse("application/json");
        String requestBody = JSONObject.toJSONString(map);
        RequestBody body = RequestBody.create(mediaType, requestBody);
        // 创建POST请求
        Request request = new Request.Builder().url(grantUrl).post(body).header("token", token).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String responseBody = response.body().string();
            JSONObject rawResult = JSONObject.parseObject(responseBody, JSONObject.class);
            System.out.println(rawResult);
            System.out.println(rawResult.get("code").toString());
            return responseBody;
        } else {
            throw new IOException("请求失败：" + response);
        }
    }


    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            String accessToken = javaSamplerContext.getParameter("access_token");
            //resData = grant();
            //断言
            JSONObject jsonObject = JSONObject.parseObject(resData);

            if (Long.parseLong(jsonObject.get("code").toString()) == 200L ||  jsonObject.get("message").toString().contains("RequestNotPermitted")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + " "+resData, SampleResult.TEXT);
                //System.out.println("获取收银台URL结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + resData, SampleResult.TEXT);
                //System.out.println("获取收银台URL结果:" + false + " " + resData);
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData + "-----失败原因: " + e.getMessage(), SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }





}
